use chrono::{DateTime, Local, Utc};
use serde::{Deserialize, Serialize};
use utoipa::{IntoParams, ToSchema};
use validator::{Validate};

use crate::data::{model::user::User, Gender};

lazy_static::lazy_static! {
    static ref PHONE_REGEX: regex::Regex = regex::Regex::new(r"^1[3-9]\d{9}$").unwrap();
}

/// 创建用户请求
#[derive(Debug, Deserialize, Validate, ToSchema)]
pub struct CreateUserRequest {
    /// 用户名
    #[schema(example = "zhangsan")]
    #[validate(length(min = 1, message = "用户名不能为空"))]
    pub username: String,

    /// 密码 自动生成默认密码（abcd@1234）
    // #[schema(example = "12345678")]
    // #[validate(length(min = 1, message = "密码不能为空"))]
    // pub password: String,

    /// 用户头像
    pub avatar: Option<String>,

    /// 性别
    /// 
    /// 可选参数：男、女、未知
    #[schema(example = "男")]
    pub gender: Gender,

    /// 手机号
    #[schema(example = "13888888888")]
    #[validate(regex(path = "*PHONE_REGEX", message = "手机号格式错误"))]
    pub phone: String,

    /// 邮箱
    #[schema(example = "zhangsan@meatball.org.cn")]
    #[validate(length(min = 1, message = "邮箱不能为空"))]
    pub email: Option<String>,

    /// 真实姓名
    #[schema(example = "张三")]
    pub nick_name: String,
}

/// 用户信息返回
#[derive(Debug, Serialize, ToSchema)]
pub struct UserInfoResponse {
    /// 用户ID
    pub id: i64,

    /// 用户名
    pub username: String,

    /// 头像
    pub avatar: Option<String>,

    /// 手机号
    pub phone: String,

    /// 邮箱
    pub email: String,

    /// 真实姓名
    pub nick_name: String,

    /// 性别
    pub gender: Gender,

    /// 是否激活
    pub is_active: bool,

    /// 创建人
    pub created_by: Option<String>,

    /// 创建时间
    #[schema(value_type = String, format = DateTime)]
    pub created_at: DateTime<Utc>,

    /// 更新时间
    #[schema(value_type = String, format = DateTime)]
    pub updated_at: DateTime<Utc>,

    /// 角色
    pub roles: Option<Vec<String>>,

    /// 菜单
    pub buttons: Option<Vec<String>>
}

impl From<User> for UserInfoResponse {
    fn from(user: User) -> Self {
        UserInfoResponse {
            id: user.id,
            username: user.username,
            avatar: user.avatar,
            phone: user.phone,
            email: user.email,
            nick_name: user.nick_name,
            gender: user.gender,
            is_active: user.is_active,
            created_by: None,
            created_at: user.created_at,
            updated_at: user.updated_at,
            roles: None,
            buttons: None
        }
    }
}

#[derive(Debug, Deserialize, IntoParams)]
#[into_params(parameter_in = Query)]
pub struct UserPageRequest {
    /// 当前页码（从1开始）
    #[param(example = 1)]
    pub current: i32,

    /// 每页条数
    #[param(example = 20)]
    pub size: i32,

    /// 用户名
    // #[param(example = "1")]
    pub username: Option<String>,

    /// 手机号
    // #[param(example = "13888888888")]
    pub phone: Option<String>
}

/// 修改用户状态请求参数
#[derive(Debug, Deserialize, Validate, ToSchema)]
pub struct ModifyStatusRequest {
    /// 用户ID
    #[schema(example = 1)]
    #[validate(range(min = 1, message = "用户ID不能为空"))]
    pub id: i64,

    /// 当前状态(修改后的值)
    #[schema(example = true)]
    #[validate(required(message = "新状态不能为空"))]
    pub is_active: Option<bool>
}